package com.businesswebsite.businesswebsite.FormalWebExplot.Controller;

import com.businesswebsite.businesswebsite.FormalWebExplot.Entity.Appointment;
import com.businesswebsite.businesswebsite.FormalWebExplot.Service.Appointment.AppointmentService;
import com.businesswebsite.businesswebsite.FormalWebExplot.Utils.RegexUtil;
import com.businesswebsite.businesswebsite.FormalWebExplot.Utils.Renum;
import com.businesswebsite.businesswebsite.FormalWebExplot.Utils.Result;
import com.businesswebsite.businesswebsite.FormalWebExplot.Utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * @Author: 段美超
 * @CreateTime: 2025-05-20
 * @Description: 预约控制器
 * @Version: 1.0
 */
@CrossOrigin(origins = "http://47.108.80.35:8080" , allowCredentials = "true")
//@CrossOrigin(origins = "http://localhost:8080" , allowCredentials = "true")
@RestController
@RequestMapping("/appointment")
public class AppointmentController {
    @Autowired
    private AppointmentService appointmentService;

    @PostMapping("/make_appointment")
    public Result makeAppointment(@RequestBody Appointment appointment){
        //赋值id
        String id = UUID.randomUUID().toString().replace("-","");
        appointment.setId(id);
        try{
            if(RegexUtil.isPhoneInvalid(appointment.getPhone_num())&&RegexUtil.isEmailInvalid(appointment.getEmail())){
                return ResultUtil.error(Renum.PHONE_OR_EMAIL_ERROR.getCode(),"手机号或邮箱格式错误");
            }
            Integer makeAppointment = appointmentService.MakeAppointment(appointment);
            if (makeAppointment > 0){
                return ResultUtil.success("预约成功");
            }
            return ResultUtil.error(Renum.UNKNOWN_ERROR.getCode(),"预约失败");
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.error(Renum.UNKNOWN_ERROR.getCode(),"请求错误，请检查网络");
        }
    }
}
